<?php

/**
 * --------------------------------
 * Default TeacherController
 *
 * 教师信息管理的统一类  TeacherController
 * 
 * --------------------------------
 */

class TeacherController extends BaseController{

	// /**
 //     * 构造函数
 //     * 统一用户Session验证
 //     * 
 //     */
 //    public function __construct(){
 //    	$check_result = Basecontroller::session('internal');
 //    	if(!$check_result) 
 //    		exit();
 //    }
	/**
	 * 查询所有教师信息接口 
	 * @param
	 * @return Response
	 */
	public function index(){

		$column = Input::get('column');
        $content = Input::get('content');
        $array = DB::table('teacher');

		//插入搜索条件
        if(null!=$column && ''!= $column && null!= $content && ''!= $content){

			$array = $array->where('teacher.'.$column, 'like', '%'.$content.'%');
        }
     	
		$array = $array
					->join('subject','subject.id','=','teacher.subject_id')
                    ->select('teacher.id','teacher.name','teacher.nativeid','teacher.sex',
                    	     'teacher.tel','teacher.birthday','teacher.introduction',
                    	     'teacher.classes_id','subject.name as subject_name')
					->paginate(15)
					->toJson();
		$array = json_decode($array);
				                   
		return Response::json(['error'=>false,'result'=>$array],200);
    }

    /**
	 * 查询单个教师信息接口 
	 * @param  $id
	 * @return Response
	 */
    public function show($id){
      
    	$teacher_result = DB::table('teacher')
    						->join('subject','teacher.subject_id','=','subject.id')
    	                    ->select('teacher.id','teacher.name','teacher.nativeid',
    	                    		 'teacher.sex','teacher.tel','teacher.birthday',
    	                    		 'teacher.introduction','teacher.subject_id',
    	                    		 'teacher.classes_id','subject.name as subject_name')
    	                    ->where('teacher.id',$id)
    	                    ->get();	
													                   
    	if(!$teacher_result){
    		return Response::json(['error'=>true,'message'=>'记录不存在'],201);
    	}else{                   
    		return Response::json(['error'=>false,'result'=>$teacher_result],200);
    	} 
    }

    /**
	 * 新增教师接口 
	 * @param 
	 * @return Response
	 */
	public function store(){

		
        if(!Input::has("name") || !Input::has("nativeid") || !Input::has("subject_id")){
	 		return Response::json(['error'=>true,'message'=>"参数错误"], 201);
	 	}

	 	if(strlen(Input::get("nativeid")) <6){
	 		return Response::json(['error'=>true,'message'=>"身份证号过短"], 201);
	 	}

	 	$new_teacher_arr = array();
		$message = Input::all();
		$teacher_message = array('name','sex','tel','birthday','nativeid','subject_id',
								 'classes_id','introduction');
		foreach ($message as $key => $value) {
			if(in_array($key,$teacher_message)){
				$new_teacher_arr[$key] = $value;
			}
		}
	 	
	 	$salt_arr = [1,2,3,4,5,6,7,8,9,0,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
	 	$salt = $salt_arr[rand(0,35)].$salt_arr[rand(0,35)].$salt_arr[rand(0,35)].$salt_arr[rand(0,35)];
	 	$password = substr(Input::get("nativeid"),-6);
	 	$new_teacher_arr['salt'] 	= $salt;
	 	$new_teacher_arr['password'] = md5($salt.$password);

	 	$id = DB::table('teacher')->insertGetId($new_teacher_arr);
	 	if($id){
	 		return Response::json(['error'=>false,'message'=>'创建成功','result'=>$id],200);	
	 	}else{
	 		return Response::json(['error'=>true,'message'=>'创建失败'],201);
	 	}
		 	
	}
    
     /**
	  * 修改教师信息接口 
	  * @param 
	  * @return Response
	  */
	 public function update($id){
	 
	 	$input_param = Input::all();
	 	
	 	$relevant_param = array('id','name','nativeid','sex','tel','birthday','introduction','subject_id');

	 	$teacher_update_arr = array();

	 	foreach ($input_param as $key => $val){
	 		if(in_array($key, $relevant_param)){
	 			$teacher_update_arr[$key] = $val;
	 		}
	 	}

	 	try{
	 		DB::table('teacher')->where('id','=',$id)->update($teacher_update_arr);
	 		return Response::json(['error'=>false,'message'=>'修改成功'],200);
	 	}catch(Exception $e){
			return Response::json(['error'=>true,'message'=>'修改失败'], 201);
		}
	 }

	

	/**
	 * 删除教师接口 
	 * @param 
	 * @return Response
	 */
	public function destroy($id){

	    $del_result = DB::table('teacher')->where('id',$id)->update(array('is_del'=>0));
	    if($del_result){
	    	return Response::json(['error'=>false,'message'=>'删除成功'],200);
	    }else{
	    	return Response::json(['error'=>true,'message'=>'删除失败'],201);
	    }

	}
}